<?php              
				include "includes".DIRECTORY_SEPARATOR."start_session.php";
				include "javascript".DIRECTORY_SEPARATOR."js_functions.php";
				include "javascript".DIRECTORY_SEPARATOR."serverside.php";
				
				if ( !defined('USERNAME') || !defined('GROUP') || GROUP != "admin" )
				{
					header("Location: manage_login.php");
					exit;
				}			



  				 $tablou = array("nume" => "",  
								 "mail" => "",
								 "parola" => "",
								 "grup" => "",
								 "listaJudete" =>""
								 );
					
				$color = array("nume" => "#333333",
								 "mail" => "#333333",
								 "parola" => "#333333",
								 "grup" => "#333333",
								 "listaJudete" => "#333333"
								 );	
				
								 		
				 $erori = "";
				 
				 $tablou_erori = array("nume1" => "Nu a fost introdus numele<br />",
				 				 "nume2" => "Nume introdus invalid<br />",
								 "mail1" => "Nu a fost introdusa adresa de mail<br />",
								 "mail2" => "Adresa de mail invalida<br />",								 
								 "parola1" => "Nu a fost introdusa parola<br />",
								 "grup1" => "Nu a fost selectat grupul<br />",
								 "listaJudete" => "Nu a fost selectat nici un judet<br />"
								 );
  
  				  require_once ('./mysql_connect.php'); 
				  $rez = mysqli_query($dbc,"SELECT * FROM groups");
				  $counties_query = mysqli_query($dbc,"SELECT name FROM account_county");

				   $group_options = "";				   
				   $group_options = $group_options."<option value=\"none\">...</option>";
				   
				   while($rand = mysqli_fetch_row($rez))
				   {
					  $group_options = $group_options."<option value='$rand[0]'>$rand[0]</option>";
				   }
				   
				   $group_counties = "";
				   $group_counties = $group_counties."<option value=\"none\">...</option>";
				   $group_counties = $group_counties."<option value='Toate judetele'>Toate judetele</option>";
				   while($county = mysqli_fetch_row($counties_query))
				   {
					  $group_counties = $group_counties."<option value='$county[0]'>$county[0]</option>";
				   }
				  

  				  $form_get=<<<GATA
	                   <form method = POST action = "admin_manage_users.php">
	                   <table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
	                   <tr><td><b>Nume</b></td><td><input type = "text" name = "nume" value='$tablou[nume]' size="40" maxlength="35" ></td></tr>
	                   <tr><td><b>Email</b></td><td> <input type = "text" name = "mail" id="userEmail" value='$tablou[mail]' size="40" maxlength="35" ></td></tr>
					   <div id="existentEmailDialog" title="Email existent" hidden="true"><p>Acest email exista deja in baza de date !</p><p>Va rugam sa introduceti un alt email !</p></div>
					   <tr><td><b>Parola</b></td><td> <input type = "text" name = "parola" id="userPassword" value='$tablou[parola]' size="40" maxlength="35" ></td></tr>
					   <tr><td><b>Grup</b></td><td> <select name="grup">'$group_options'</select></td></tr>
					   <tr><td><b>Judet</b></td>
					   	   <td> <select name="judet_utilizator_nou" id="selectedCountyForUser">'$group_counties'</select>
						   <input type="button" id="addUserCountyButton" value="Adauga judet"/></td>
					    </tr>
						<tr>
							<td></td>
							<td>
							<textarea id="countiesAreaAddUser" readonly="true" name="listaJudete" title="Judetele asociate acestui utilizator"  rows="7" cols="20">$tablou[listaJudete]</textarea>
							</td>
						</tr>
					   <tr><td></td><td><input type="submit" id="addNewUser" value = "Adauga Utilizator" ></td></tr>
					   
					   </table>
	                   </form>
GATA;


			   if($_SERVER['REQUEST_METHOD'] == 'GET')
			   {
				   	echo $form_get;
			   }
			   
  				  if($_SERVER['REQUEST_METHOD'] == 'POST')
				  
				  {
				  		$ok = 1;
								
							#verifica numele
							if(empty($_POST["nume"]))
							{
									$erori = $erori.$tablou_erori["nume1"];
									$ok = 0;
									$color["nume"] = "red";
							}
							else if((preg_match('/^[A-Za-z]+([ ,\'][A-Za-z-]+)*$/',$_POST["nume"])) == 0)
							{
									$erori = $erori.$tablou_erori["nume2"];
									$ok = 0;		
									$color["nume"] = "red";								
							}
							else
							{
									$tablou["nume"] = $_POST["nume"];  		
							}
							
						
							#verifica mail
							if(empty($_POST["mail"]))
							{
								$erori = $erori.$tablou_erori["mail1"];
								$ok = 0;							
								$color["mail"] = "red";																					
							}   
							else if((preg_match('/^([.0-9a-z_-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/',$_POST["mail"])) == 0)
							{
								$erori = $erori.$tablou_erori["mail2"];
								$ok = 0;						
								$color["mail"] = "red";																																			
							}
							else
							{
								$tablou["mail"] = $_POST["mail"];  
							}
							
					
							#verifica parola
							if(empty($_POST["parola"]))
							{
								$erori = $erori.$tablou_erori["parola1"];
								$ok = 0;		
								$color["parola"] = "red";								
							}
							else
							{
								$tablou["parola"] = $_POST["parola"];  
							}
							
							#verifica grupul
							if(empty($_POST["grup"]) || ( $_POST["grup"] == "none") )
							{
								$erori = $erori.$tablou_erori["grup1"];
								$ok = 0;		
								$color["grup"] = "red";								
							}
							else
							{
								$tablou["grup"] = $_POST["grup"];  
							}
							
							#verifica judetele
							if(empty($_POST["listaJudete"]) || ( $_POST["listaJudete"] == "none") )
							{
								
								$erori = $erori.$tablou_erori["listaJudete"];
								$ok = 0;		
								$color["listaJudete"] = "red";								
							}
							else
							{
								$tablou["listaJudete"] = $_POST["listaJudete"]; 
								
							}
							
							if($ok != 1)
							{
					
							  require_once ('./mysql_connect.php'); 
							  $rez = mysqli_query($dbc,"SELECT * FROM groups");

							  $group_options = "";				   
							  $group_options = $group_options."<option value=\"none\">...</option>";
			   
							  while($rand = mysqli_fetch_row($rez))
							  {							  
								  if( $tablou['grup'] == $rand[0] )
								  {
								  	 $group_options = $group_options."<option selected=\"selected\" value='$rand[0]'>$rand[0]</option>";
									
								  }
								  else
									 $group_options = $group_options."<option value='$rand[0]'>$rand[0]</option>";
							  }
							  
							   $group_counties = "";
				  			   $group_counties = $group_counties."<option value=\"none\">...</option>";
							   $group_counties = $group_counties."<option value=\"All\">Toate judetele</option>";
							   $counties_query = mysqli_query($dbc,"SELECT name FROM account_county");
							 
							
							   while($county1 = mysqli_fetch_row($counties_query))
					 				 $group_counties = $group_counties."<option value='$county1[0]'>$county1[0]</option>";
				   				
								
								
								
					   			$form_post=<<<GATA
			                   <form method = POST action = "admin_manage_users.php">
			                   <table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
			                   <tr><td><b><font color='$color[nume]'>Nume</font></b></td><td><input type = "text" name = "nume" value='$tablou[nume]' size="40" maxlength="35" ></td></tr>
	    		               <tr><td><b><font color='$color[mail]'>Email</font></b></td><td> <input type = "text" id="userEmail" name = "mail" value='$tablou[mail]' size="40" maxlength="35" ></td></tr>
							   <div id="existentEmailDialog" title="Email existent" hidden="true"><p>Acest email exista deja in baza de date !</p><p> Va rugam sa introduceti un alt email !</p></div>
							   <tr><td><b><font color='$color[parola]'>Parola</font></b></td><td> <input type = "text" name = "parola" value='$tablou[grup]' size="40" maxlength="35" ></td></tr>						   <tr><td><b><font color='$color[parola]'>Grup</font></b></td><td> <select name="grup">'$group_options'</select></td></tr>
							  <tr>
							  		<td><b><font >Judet</font></b></td>
							  		<td> <select name="judet_utilizator_nou" id="selectedCountyForUser">'$group_counties'</select>
									<input type="button" id="addUserCountyButton" value="Adauga judet"/></td>
								  </tr>
							 	 <tr>
									<td></td>
									<td><textarea id="countiesAreaAddUser"  name="listaJudete" title="Judetele asociate acestui utilizator"  rows="7" cols="20">$tablou[listaJudete]</textarea>
									</td>
								</tr>
							   <tr>
							   		<td></td>
									<td><input type = submit value = "Adauga Utilizator"></td>
								</tr>
					   
							   </table>
	            		       </form>
GATA;
					 			echo $form_post;
								echo "<font color=\"red\">".$erori."</font>";
							}
							else  
							{
									require_once ('mysql_connect.php');
									$localCounties = array();
									$countiesAndIdsQuery = getAllCountiesAndIds($dbc);
									while($countyRetrv = mysqli_fetch_assoc($countiesAndIdsQuery)){
										$localCounties[$countyRetrv["name"]] = $countyRetrv["id"];
									}
									//array in care sunt retinute id-urile judetelor
									$countiesIds = array();
									$countiesNames = preg_split("/[\s]+/",$tablou["listaJudete"]);
									
									if (in_array("Toate",$countiesNames) && in_array("judetele",$countiesNames) ){
										foreach ($localCounties as $k => $v) 
											array_push($countiesIds,$localCounties[$k] );
										
									}
									else{
										foreach ($countiesNames as $county1){
											if (array_key_exists($county1, $localCounties)){
												if (!in_array($localCounties[$county1], $countiesIds))
													array_push($countiesIds,$localCounties[$county1] );
											}
										}
									}
									//string cu lista de id-uri de judete separate prin ","
									$commaSeparatedCountiesIds = implode(",", $countiesIds);
									
									
									$insert_user = mysqli_query($dbc,"INSERT INTO utilizatori (nume,parola,mail,grup,judete) VALUES ('$tablou[nume]','$tablou[parola]','$tablou[mail]','$tablou[grup]','$commaSeparatedCountiesIds')"); 
									
									
									
									if ($insert_user ){
										echo "<br />";	
										echo "<center><h2><font color=\"#00CC00\">Utilizatorul a fost adaugat cu succes ! </font></h2></center>";	
									}
									else
									{
										echo "<br />";	
										echo "<center><h2><font color='red'>A aparut o problema la adaugarea utilizatorului!\nVa rugam reincercati!</font></h2></center>";
									}
									
									echo "<br />";	
									echo $form_get;
							}
							
					}
						
?>


